/**
 * Delete Favorite
 */

var DelFav = function(id, options){
	this.id = id;
	this.error = options.error;
	this.success = options.success;
	this.delete();
};

DelFav.prototype._success = function(data){
	if(typeof this.success == 'function') {
		this.success(data);
	}
};

DelFav.prototype._error = function(code, message){
	if(typeof this.error == 'function') {
		this.error(code, message);
	}
};

DelFav.prototype.check = function(nextStatement){
	var self = this;
	Mysql.query("SELECT COUNT(*) FROM Favorite WHERE id="+Mysql.escape(self.id)+" LIMIT 1")
	.on('error', function(error){
		console.log('Mysql error: '+error);
		self._error(9999, 'Mysql error');
	})
	.on('result', function(result){
		if(result['COUNT(*)'] == 0) {
			self._error(9999, 'Not found favorite');
			return;
		}
		nextStatement();
	});
};

DelFav.prototype.delete = function(){
	var self = this;
	var id = Mysql.escape(self.id);
	self.check(function(){
		Mysql.query("DELETE FROM Favorite WHERE id="+id)
		.on('error', function(error){
			console.log('Mysql error: '+error);
			self._error(9999, 'Mysql error');
		})
		.on('result', function(result){
			self._success(result);
		});
	});
};

module.exports = DelFav;